package Controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import dao.*;

import Model.Person;
import Model.Phone;

public class personController implements Controller
{
	private DAO model;
	private ArrayList<Person> listPerson = new ArrayList<Person>();

	public personController()
	{
		model = new DAO_H2();
		setActionReadall();
	}



	@Override
	public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse respons ) throws Exception
	{
		String buffer;
		String id;
		
		request.setCharacterEncoding("UTF-8");
		
		buffer = request.getRequestURL().toString();
		buffer = buffer.substring(buffer.indexOf(request.getContextPath())+11, buffer.length());
		
		System.out.println( "url = " +request.getRequestURL() );
		
		if ( buffer.equals("index.htm") )
		{
			return new ModelAndView( "viewPerson", "listPerson" , listPerson );
		}
		
		if ( buffer.length()>7 && buffer.substring(0,6).equals("delete") )
		{
			id=buffer.substring(6,buffer.indexOf(".htm"));
			setActionDelete(Integer.parseInt(id));
			setActionReadall();
			return new ModelAndView( "viewPerson", "listPerson" , listPerson );
		}
		
		if ( buffer.length()>5 && buffer.substring(0,4).equals("edit") )
		{
			if (request.getParameter("id")!=null)
			{
				setActionUpdate(newPerson( request ));
				setActionReadall();
			}
			
			id=buffer.substring(4,buffer.indexOf(".htm"));
			return new ModelAndView( "editPerson", "person" , findPerson( Integer.parseInt(id) ) );
		}
		
		if ( buffer.equals("newPerson.htm") )
		{
			if (request.getParameter("id")!=null)
			{
				setActionCreate( newPerson( request ) );
				setActionReadall();
			}
			
			id=buffer.substring(4,buffer.indexOf(".htm"));
			return new ModelAndView( "editPerson", "person" , getNewPerson() );
		}
		
		return new ModelAndView( "errorPage", "error404", null );
	}
	
	public void setActionReadall()
	{
		try 
		{
			model.Connect();
			listPerson = model.ReadAll();
			model.Disconnect();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public Person findPerson( int id )
	{
		Person person = new Person();
		
		for (Person tmp : listPerson)
		{
			if ( tmp.getId()==id )
			{
				return tmp;
			}
		}
		return person;
	}

	public void setActionDelete( int id )
	{
		if (!listPerson.isEmpty())
		{
			try 
			{
				model.Connect();
				model.Delete(id);
				model.Disconnect();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}
	
	public void setActionUpdate(Person person)
	{
		try 
		{
			model.Connect();
			model.Update(person);
			model.Disconnect();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public void setActionCreate(Person person)
	{
		ArrayList<Person> persons = new ArrayList<Person>();
		persons.add(person);
		
		try 
		{
			model.Connect();
			model.Create(persons);
			model.Disconnect();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public Person getNewPerson()
	{
		int id=0;
		
		Person person = new Person();
		
		try 
		{
			model.Connect();
			id=model.MaxId();
			model.Disconnect();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		person.setId(id+1);

		return person;
	}
	
	public Person newPerson( HttpServletRequest request )
	{
		Person person = new Person();
		ArrayList<Phone> phones = new ArrayList<Phone>();
		String phone = "phone[0].phone";;
		int i=0;
		
		person.setId( Integer.parseInt(request.getParameter("id")) );
		person.setFistname(request.getParameter("fistname"));
		person.setSurname(request.getParameter("surname"));
		person.setAge( Integer.parseInt(request.getParameter("age")) );
		
		while(request.getParameter(phone)!=null)
		{
			phones.add(new Phone( request.getParameter(phone) ));
			phone = "phone["+i+"].phone";
			i++;
		}
		
		if (!phones.isEmpty())
		{
			phones.remove(0);
		}
		
		person.setPhone(phones);
		
		return person;
	}

}
